using System;
using System.Collections.Generic;
using UnityEngine;

namespace NJG
{
	public abstract class UIMiniMapBase : UIMapBase
	{
		public enum ZoomType
		{
			ZoomIn = 0,
			ZoomOut = 1
		}

		private static UIMiniMapBase mInst;

		public static UIMiniMapBase inst;

		public Vector3 targetAngle;

		public KeyCode lockKey = KeyCode.L;

		public GameObject northIcon;

		public KeyCode mapKey = KeyCode.M;

		public int overlayBorderOffset;

		private bool worldMapVisible;

		private GameObject northRoot;

		protected Vector2 mArrowScale;

		protected Transform mArrowRoot;

		protected List<NJGMapItem> mPingList = new List<NJGMapItem>();

		protected List<NJGMapItem> mPingUnused = new List<NJGMapItem>();

		protected List<UIMapArrow> mListArrow = new List<UIMapArrow>();

		protected List<UIMapArrow> mUnusedArrow = new List<UIMapArrow>();

		private NJGMapItem pingMarker;

		protected int mArrowCount;

		private Pivot mPivot;

		private Vector2 mMargin;

		public static Action<Vector3> onMapClick;

		protected Vector3 miniMapLeftUp;

		protected Vector3 miniMapRightUp;

		protected Vector3 miniMapLeftDown;

		protected Vector3 miniMapRightDown;

		private float maxDistanceForTargetSquare;

		private Vector3 mLastTargetPosition;

		private float targetVelocity;

		private Vector2 prevPos;

		private float timeSpeed;

		private float timeMeterSpeed = 0.2f;

		private float fScaleMap;

		public static bool initialized
		{
			get
			{
				return mInst != null;
			}
		}

		public Transform arrowRoot
		{
			get
			{
				if (mArrowRoot == null && Application.isPlaying)
				{
					mArrowRoot = NJGTools.AddChild(base.gameObject).transform;
					mArrowRoot.parent = iconRootParent;
					mArrowRoot.name = "_MapArrows";
					mArrowRoot.localEulerAngles = Vector3.zero;
					mArrowRoot.localScale = Vector3.one;
					mArrowRoot.localPosition = Vector3.zero;
				}
				return mArrowRoot;
			}
		}

		protected override Vector3 TargetForward
		{
			get
			{
				if (PlayerBehavior.MyPlayer != null)
				{
					if (PlayerBehavior.MyPlayer.inCar && PlayerBehavior.MyPlayer.currentCar.Type != CarType.Tank && PlayerBehavior.MyPlayer.currentCar.Type != CarType.Titan)
					{
						return PlayerBehavior.MyPlayer.currentCar.transform.forward;
					}
					return GameController.instance.cameraGame.transform.forward;
				}
				return Vector3.right;
			}
		}

		protected override void Awake()
		{
			base.Awake();
			inst = this;
		}

		protected override void OnDestroy()
		{
			base.OnDestroy();
			inst = null;
		}

		protected override void OnStart()
		{
			base.OnStart();
			if (Application.isPlaying)
			{
				northRoot = NJGTools.AddChild(base.iconRoot.gameObject);
				northRoot.name = "North";
				northRoot.transform.localPosition = Vector3.zero;
				if (northIcon != null)
				{
					northIcon.transform.parent = northRoot.transform;
					northIcon.transform.localRotation = Quaternion.identity;
				}
				if (calculateBorder)
				{
					mapBorderRadius = rendererTransform.localScale.x / 2f / 4f;
				}
			}
			UpdateAlignment();
		}

		public virtual void UpdateAlignment()
		{
			Vector3 zero = Vector3.zero;
			zero.z = rendererTransform.localPosition.z;
			if (pivot != Pivot.Center)
			{
				switch (pivot)
				{
				case Pivot.TopRight:
					zero.x = Mathf.Round(-0.5f * mapScale.x) - margin.x;
					zero.y = Mathf.Round(-0.5f * mapScale.y) - margin.y;
					break;
				case Pivot.Right:
					zero.x = Mathf.Round(-0.5f * mapScale.x) - margin.x;
					break;
				case Pivot.BottomRight:
					zero.x = Mathf.Round(-0.5f * mapScale.x) - margin.x;
					zero.y = Mathf.Round(0.5f * mapScale.y) + margin.y;
					break;
				case Pivot.Bottom:
					zero.y = Mathf.Round(0.5f * mapScale.y) + margin.y;
					break;
				case Pivot.Top:
					zero.y = Mathf.Round(-0.5f * mapScale.y) - margin.y;
					break;
				case Pivot.TopLeft:
					zero.x = Mathf.Round(0.5f * mapScale.x) + margin.x;
					zero.y = Mathf.Round(-0.5f * mapScale.y) - margin.y;
					break;
				case Pivot.Left:
					zero.x = Mathf.Round(0.5f * mapScale.x) + margin.x;
					break;
				case Pivot.BottomLeft:
					zero.x = Mathf.Round(0.5f * mapScale.x) + margin.x;
					zero.y = Mathf.Round(0.5f * mapScale.y) + margin.y;
					break;
				}
			}
			rendererTransform.localPosition = zero;
		}

		protected override Transform GetIconRootParent()
		{
			Transform transform = NJGTools.AddChild(base.gameObject).transform;
			if (iconRootParent != null)
			{
				transform.parent = iconRootParent;
				transform.localPosition = new Vector3(0f, 0f, 1f);
			}
			transform.name = "_MapIcons";
			return transform;
		}

		protected override void SetIconPosition(UIMapIconBase icon)
		{
			base.SetIconPosition(icon);
			icon.cachedTransform.localScale = Vector3.one * base.scaleOnMoving;
		}

		protected void SetIconRoration(UIMapIconBase icon, NJGMapItem item, Vector3 eu)
		{
			Transform transform = icon.cachedTransform;
			if (item == null || icon == null)
			{
				return;
			}
			if (!item.rotate && rotateWithPlayer)
			{
				if (!transform.localEulerAngles.Equals(eu))
				{
					transform.localEulerAngles = eu;
				}
			}
			else if (item.rotate && (bool)item.cachedTransform)
			{
				float z = ((!(Vector3.Dot(item.cachedTransform.forward, Vector3.Cross(Vector3.up, Vector3.forward)) <= 0f)) ? (-1f) : 1f) * Vector3.Angle(item.cachedTransform.forward, Vector3.forward);
				transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, z);
			}
		}

		protected override void UpdateIconsAfterMapShift(Vector2 shift)
		{
			bool flag = !shift.Equals(Vector2.zero);
			Vector3 zero = Vector3.zero;
			if (flag)
			{
				zero.x = shift.x * map.bounds.size.x * scaleZoom.x;
				zero.y = shift.y * map.bounds.size.z * scaleZoom.y;
			}
			Vector3 eu = new Vector3(0f, 0f, 0f - base.iconRoot.localEulerAngles.z);
			int num = mList.Count;
			while (num > 0)
			{
				UIMapIconBase uIMapIconBase = mList[--num];
				if (uIMapIconBase.isValid)
				{
					if (flag)
					{
						uIMapIconBase.cachedTransform.localPosition += zero;
					}
					SetIconRoration(uIMapIconBase, uIMapIconBase.item, eu);
				}
			}
			float sinA = Mathf.Sin(-(float)Math.PI / 180f * mapAngle);
			float cosA = Mathf.Cos(-(float)Math.PI / 180f * mapAngle);
			for (int i = 0; i < NJGMapItem.list.Count; i++)
			{
				NJGMapItem nJGMapItem = NJGMapItem.list[i];
				if (nJGMapItem.haveArrow && nJGMapItem.arrow != null && nJGMapItem.arrow.isValid)
				{
					if (!NJGTools.GetActive(nJGMapItem.arrow.gameObject))
					{
						NJGTools.SetActive(nJGMapItem.arrow.gameObject, true);
					}
					nJGMapItem.arrow.UpdatePosition(WorldToMap(nJGMapItem.cachedTransform.position), mapHalfScale, sinA, cosA);
				}
			}
		}

		protected override void UpdateIconsList()
		{
			int num = mList.Count;
			while (num > 0)
			{
				UIMapIconBase uIMapIconBase = mList[--num];
				uIMapIconBase.isValid = false;
			}
			CalculatePoints();
			for (int i = 0; i < NJGMapItem.list.Count; i++)
			{
				NJGMapItem nJGMapItem = NJGMapItem.list[i];
				if (nJGMapItem.type >= 1)
				{
					UpdateIcon(nJGMapItem);
				}
			}
		}

		private bool PointInTriangle(Vector3 position, Vector3 a, Vector3 b, Vector3 c)
		{
			float num = (a.x - position.x) * (b.z - a.z) - (b.x - a.x) * (a.z - position.z);
			float num2 = (b.x - position.x) * (c.z - b.z) - (c.x - b.x) * (b.z - position.z);
			float num3 = (c.x - position.x) * (a.z - c.z) - (a.x - c.x) * (c.z - position.z);
			return (num >= 0f && num2 >= 0f && num3 >= 0f) || (num <= 0f && num2 <= 0f && num3 <= 0f);
		}

		protected void CalculatePoints()
		{
			float f = (float)Math.PI / 180f * mapAngle;
			float cosA = Mathf.Cos(f);
			float sinA = Mathf.Sin(f);
			float x = mapHalfScale.x;
			float y = mapHalfScale.y;
			miniMapLeftDown = TurnOnAngle(0f - x, 0f - y, cosA, sinA);
			miniMapRightUp = TurnOnAngle(x, y, cosA, sinA);
			miniMapRightDown = TurnOnAngle(x, 0f - y, cosA, sinA);
			miniMapLeftUp = TurnOnAngle(0f - x, y, cosA, sinA);
			if (target != null)
			{
				maxDistanceForTargetSquare = (miniMapLeftUp - target.position).sqrMagnitude;
			}
		}

		private Vector3 TurnOnAngle(float x, float y, float cosA, float sinA)
		{
			float x2 = x * cosA - y * sinA;
			float y2 = y * cosA + x * sinA;
			return MapToWorld(new Vector2(x2, y2));
		}

		protected bool IsVisible(Vector3 position)
		{
			if (target == null)
			{
				return false;
			}
			return (position - target.position).sqrMagnitude < maxDistanceForTargetSquare;
		}

		public bool IsExactVisible(Vector3 position)
		{
			if (target == null)
			{
				return false;
			}
			return (position - target.position).sqrMagnitude < maxDistanceForTargetSquare && (PointInTriangle(position, miniMapLeftUp, miniMapRightUp, miniMapLeftDown) || PointInTriangle(position, miniMapRightUp, miniMapRightDown, miniMapLeftDown));
		}

		protected override void UpdateIcon(NJGMapItem item)
		{
			if (item.dontShowIcon)
			{
				return;
			}
			bool flag = ((!item.haveArrow) ? IsVisible(item.cachedTransform.position) : IsExactVisible(item.cachedTransform.position));
			if (!base.isPanning && item.haveArrow)
			{
				if (flag)
				{
					if (item.arrow != null)
					{
						if (NJGTools.GetActive(item.arrow.gameObject))
						{
							NJGTools.SetActive(item.arrow.gameObject, false);
						}
						item.arrow.isValid = false;
					}
				}
				else
				{
					if (item.arrow == null)
					{
						item.arrow = GetArrow(item);
					}
					if (item.arrow != null)
					{
						item.arrow.isValid = true;
					}
				}
			}
			if (!item.haveArrow && item.arrow != null && inst != null)
			{
				inst.DeleteArrow(item.arrow);
			}
			if (flag)
			{
				UIMapIconBase entry = GetEntry(item);
				if (entry != null && !entry.isValid)
				{
					SetIconPosition(entry);
					entry.isMapIcon = false;
					entry.isValid = true;
				}
			}
		}

		protected virtual UIMapArrow GetArrow(UnityEngine.Object o)
		{
			return (UIMapArrow)o;
		}

		protected override void Update()
		{
			if (mPivot != pivot || mMargin != margin || mMapScale != mapScale)
			{
				mMapScale = mapScale;
				mPivot = pivot;
				mMargin = margin;
				UpdateAlignment();
			}
			if (arrowRoot != null)
			{
				if (base.isPanning && arrowRoot.localScale != new Vector3(0.001f, 0.001f, 0.001f))
				{
					arrowRoot.localScale = new Vector3(0.001f, 0.001f, 0.001f);
				}
				else if (!base.isPanning && arrowRoot.localScale != Vector3.one)
				{
					arrowRoot.localScale = Vector3.one;
				}
			}
			UpdateKeys();
			if (target != null)
			{
				if (timeSpeed > timeMeterSpeed)
				{
					Vector2 vector = new Vector2(target.position.x - mLastTargetPosition.x, target.position.z - mLastTargetPosition.z);
					fScaleMap = Mathf.Clamp(1f / (1f + scaleOnMovingFactor * vector.magnitude), 0.6f, 1f);
					mLastTargetPosition = target.position;
					timeSpeed -= timeMeterSpeed;
				}
				timeSpeed += Time.deltaTime;
				base.scaleOnMoving = Mathf.Lerp(base.scaleOnMoving, fScaleMap, Time.deltaTime);
			}
			base.Update();
			int num = mList.Count;
			while (num > 0)
			{
				UIMapIconBase uIMapIconBase = mList[--num];
				if (uIMapIconBase.isValid)
				{
					NJGMapItem item = uIMapIconBase.item;
					if (item.type == 1 || item.type == 22)
					{
						uIMapIconBase.cachedTransform.localPosition = Vector3.zero;
					}
				}
			}
		}

		protected virtual void UpdateKeys()
		{
			if (Input.GetKeyDown(mapKey))
			{
				ToggleWorldMap();
			}
			if (Input.GetKeyDown(lockKey))
			{
				rotateWithPlayer = !rotateWithPlayer;
			}
		}

		public virtual void DeleteArrow(UIMapArrow ent)
		{
			if (ent != null)
			{
				mListArrow.Remove(ent);
				mUnusedArrow.Add(ent);
				NJGTools.SetActive(ent.gameObject, false);
			}
		}

		public void ToggleWorldMap()
		{
			if (UIWorldMapBase.inst != null)
			{
				UIWorldMapBase.inst.Toggle();
			}
		}
	}
}
